<?php
namespace app\modules\supplier\logic;
use app\modules\base\LogicBase;
use think\Db;


class SupplierUserLogic extends LogicBase
{
    //验证
//    protected $valid;

    public function _initialize()
    {
//        $this->valid = validate('app\modules\supplier\validate\SupplierPost'); //游客验证
    }

    /**
     * 获取供应商信息
     * @param integer $id 参数
     * @return bool|array
     */
    public function get($id = null){
        if (empty($id) || !Db::name('supplier_user')->where(['id'=>$id])->find()){
            $this->_error[] ='无效参数';
            return false;
        }

        $data = Db::name('supplier_user')->where(['id'=>$id])->find();
        return $data;

    }
    /**
         * 修改供应商账户信息
         * @param integer $id ID
         * @param array $data 参数
         * @return bool|array
         */
        public function edit_info($id = null,$data=[]){
            if (empty($id) || !Db::name('supplier_user')->where(['id'=>$id])->find()){
                $this->_error[] ='无效参数';
                return false;
            }

            if (empty($data['username'])){
                $this->_error[]='供应商账号不为空';
                return false;
            }

            if (!preg_match('/^1[3-5,7,8]{1}[0-9]{9}$/', $data['username'])) {
                $this->_error[]='供应商用户账户格式不正确';
                return false;
            }

            if (Db::name('supplier_user')->where(['id'=>['neq',$id],'username'=>$data['username']])->find()){
                $this->_error[]='供应商账户已存在';
                return false;
            }

            $res = Db::name('supplier_user')->where(['id'=>$id])->update($data);

            if ($res!==false){
                return true;
            }else{
                return false;
            }

    }

    /**
     * 修改密码
     * @param integer $id ID
     * @param array $data 参数
     * @return bool|array
     */
    public function edit_password($id= null,$data=[]){
        if (empty($id) || !Db::name('supplier_user')->where(['id'=>$id])->find()){
            $this->_error[] ='无效参数';
            return false;
        }

        if (empty($data['old_password'])){
            $this->_error[] ='请填写原密码';
            return false;
        }

        if (empty($data['password'])){
            $this->_error[] ='请填写新密码';
            return false;
        }

        if (empty($data['repeat_password'])){
            $this->_error[] ='请重复新密码';
            return false;
        }

        if (!Db::name('supplier_user')->where(['id'=>$id,'password'=>md5($data['old_password'])])->find()){
            $this->_error[] ='原密码填写错误，如遗忘，请尝试忘记密码找回！';
            return false;
        }

        if ($data['password']!==$data['repeat_password']){
            $this->_error[]='新密码两次填写不一致';
            return false;
        }

        $res = Db::name('supplier_user')->where(['id'=>$id])->setField('password',md5($data['password']));

        if ($res){
            return true;
        }else{
            return false;
        }


    }

    /**
     * 修改供应商账户信息
     * @param array $username 用户名 手机号码
     * @return bool|array
     */
    public function edit_forget($username){

        if (empty($username)){
            $this->_error[]='供应商账号不为空';
            return false;
        }

        if (!Db::name('supplier_user')->where(['username'=>$username])->find()){
            $this->_error[]='供应商账户不存在';
            return false;
        }

        //生成随机密码 修改密码 发送给用户提醒登录
        $password = get_rand_pass(6);

        if (empty($password)){
            $this->_error[]='密码生成失败';
            return false;
        }

        $res =  Db::name('supplier_user')->where(['username'=>$username])->setField('password',md5($password));//修改数据库密码

        //发送短信
        $this->sendTmp($username,$password);

        if ($res!==false){
            return true;
        }else{
            return false;
        }

    }

    /**
     * 发送模板消息
     * @param  [type] $info [description]
     * @return [type]        [description]
     */

    protected function sendTmp($username,$password){
        import('Alidayu.TopSdk');
        import('Alidayu.top.TopClient');
        import('Alidayu.top.request.AlibabaAliqinFcSmsNumSendRequest');
        $appkey="23535194";
        $secret="48953173553ad6ad6b306138f518d7f1";

        $product ='供应商管理平台';

        $c = new \TopClient();
        $c ->appkey = $appkey ;
        $c ->secretKey = $secret ;
        $req = new \AlibabaAliqinFcSmsNumSendRequest();
        $req ->setExtend( "" );
        $req ->setSmsType( "normal" );
        $req ->setSmsFreeSignName( "速冻在线" );
        $req ->setSmsParam("{name:'".$username."',product:'".$product."',password:'".$password."'}");
        $req ->setRecNum( $username );
        $req ->setSmsTemplateCode( "SMS_116475075" );
        $re = $c ->execute( $req );
    }
}